#include "EWAUITimelinePlusWidget.hpp"

namespace Eyer
{
    bool EWAUITimelinePlusWidget::OnMouseMoveEvent (const Vec2I & pos)
    {
        if(segBar->IsDraging()){
            int y = pos.y();
            int height = y - 40;
            if(height <= 0){
                height = 0;
            }
            videoTrackLayout->SetMinHeight(height);
        }
        return true;
    }

    Vec2I EWAUITimelinePlusWidget::OnMeasure(int suggestWidth, int suggestHeight, int level)
    {
        EWAUIWidget::OnMeasure(suggestWidth, suggestHeight, level);
        int h = videoTrackHeader->wh.height();
        videoTrackBodyLayout->SetMinHeight(h);
        const Vec2I & res = EWAUIWidget::OnMeasure(suggestWidth, suggestHeight, level);
        return res;
    }

    EWAUITimelinePlusWidget::EWAUITimelinePlusWidget(const EyerSmartPtr<EWAUITimelineContext> & _timelineCtx, EWAUILayoutParams width, EWAUILayoutParams height)
        : EWAUIWidget(width, height)
        , timelineCtx       (_timelineCtx)
        , ruleLayout        (new EWAUILayout(LT::LinearLayoutHorizontal,LP::MATCH_PARENT, LP::WRAP_CONTENT))
        , ruleHeaderLayout  (new EWAUILayout(LT::LinearLayoutHorizontal,LP::WRAP_CONTENT,LP::WRAP_CONTENT))
        , ruleWidget        (new EWAUIRuleWidget(LP::MATCH_PARENT, LP::WRAP_CONTENT))

        , segBar            (new EWAUIWidgetVerticalSegBar(LP::MATCH_PARENT, LP::WRAP_CONTENT))

        , videoTrackLayout  (new EWAUIWidgetScrollVideoTrack(ScrollType::VERTICAL_INVERT, LT::LinearLayoutHorizontal, LP::MATCH_PARENT, LP::FIXED))
        , videoTrackHeader  (new EWAUITrackHeaderVideo(timelineCtx, LP::WRAP_CONTENT, LP::WRAP_CONTENT))
        , videoTrackBodyLayout  (new EWAUIWidgetScroll(ScrollType::HORIZONTAL, LT::LinearLayoutHorizontal, LP::WRAP_CONTENT, LP::FIXED))
        , videoTrack        (new EWAUITrackBodyVideo(timelineCtx, LP::WRAP_CONTENT, LP::WRAP_CONTENT))

        , audioTrackLayout  (new EWAUIWidgetScroll(ScrollType::VERTICAL, LT::LinearLayoutHorizontal, LP::MATCH_PARENT, LP::WRAP_CONTENT))
        , audioTrackHeader  (new EWAUITrackHeaderAudio(timelineCtx, LP::WRAP_CONTENT, LP::WRAP_CONTENT))
        , audioTrackBodyLayout  (new EWAUIWidgetScroll(ScrollType::HORIZONTAL, LT::LinearLayoutHorizontal, LP::WRAP_CONTENT, LP::MATCH_PARENT))
        , audioTrack        (new EWAUITrackBodyAudio(timelineCtx, LP::WRAP_CONTENT, LP::WRAP_CONTENT))
    {
        SetWidgetName("EWAUITimelinePlusWidget");
        SetLayoutType(EWAUILayoutType::LinearLayoutVertical);
        ruleLayout->SetWidgetName("EWAUIRuleLayout");
        {
            ruleHeaderLayout->SetWidgetName("EWAUIRuleHeaderLayout");
            ruleHeaderLayout->SetMinWH(200, ruleWidget->ruleHeight);
            ruleHeaderLayout->SetBGColor(EyerColor::GRAY_34);
            ruleLayout->AddWidget(ruleHeaderLayout);
            ruleLayout->AddWidget(ruleWidget);
        }
        AddWidget(ruleLayout);

        // 添加视频轨道 UI
        videoTrackLayout->SetBGColor(EyerColor::BLUE);
        videoTrackLayout->SetMinHeight(300);
        {
            videoTrackLayout->AddContentWidget(videoTrackHeader);
            videoTrackLayout->AddContentWidget(videoTrackBodyLayout);
            {
                videoTrackBodyLayout->AddContentWidget(videoTrack);
            }
        }
        AddWidget(videoTrackLayout);

        // 添加分割条
        AddWidget(segBar);

        // 添加音频轨道 UI
        audioTrackLayout->SetBGColor(EyerColor::GREEN);
        {
            audioTrackLayout->AddContentWidget(audioTrackHeader);
            audioTrackLayout->AddContentWidget(audioTrackBodyLayout);
            {
                audioTrackBodyLayout->AddContentWidget(audioTrack);
            }
        }
        AddWidget(audioTrackLayout);
    }

    EWAUITimelinePlusWidget::~EWAUITimelinePlusWidget()
    {

    }

    int EWAUITimelinePlusWidget::AddTrack(const EyerString & trackId)
    {
        EyerSmartPtr<EWAPrjTrackUI> videoTrack(new EWAPrjTrackUI(trackId, EWAPrjTrackType::VIDEO));
        timelineCtx->timeline.AddTrackBack(videoTrack, EWAPrjTrackType::VIDEO);

        EyerSmartPtr<EWAPrjTrackUI> audioTrack(new EWAPrjTrackUI(trackId, EWAPrjTrackType::AUDIO));
        timelineCtx->timeline.AddTrackBack(audioTrack, EWAPrjTrackType::AUDIO);

        return 0;
    }

    int EWAUITimelinePlusWidget::InsertFragment(const EyerSmartPtr<EWAPrjFragmentFragmentResourse> & fragment, int trackId)
    {
        if(trackId < 0){
            return -1;
        }
        if(trackId >= timelineCtx->timeline.GetTrackCount(EWAPrjTrackType::AUDIO)){
            return -2;
        }

        EyerSmartPtr<EWAPrjTrack> track = timelineCtx->timeline.GetTrack(trackId, EWAPrjTrackType::AUDIO);
        track->PushFragmentBack(fragment);

        return 0;
    }
}